<?php
/**
 * 后台_管理员_控制器
 * @property Admin_mdl $admin
 * @property Admin_role_mdl $role
 */
class Manager extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('Admin_mdl','admin');
		$this->load->model('Admin_role_mdl','role');
		$this->load->model('School_mdl','school');
		$this->load->helper('category');
	}
	/**
	 * 视图:管理员列表
	 */
	public function index(){
		// 获取管理员列表
		$adminList = $this->admin->getList();
		$adminList = array_combine( array_column($adminList,'id'),$adminList );
		// 获取角色列表
		$roleList = $this->role->getList();
		$roleList = array_combine( array_column($roleList,'id'),$roleList );
		// 获取管理员与角色映射列表
		$adminRoleMap = $this->admin->getAdminRoleMap();
		
		foreach ($adminRoleMap as $v){
			$uid = $v['uid'];
			$rid = $v['rid'];
			if ( isset($adminList[$uid]) && isset($roleList[$rid]) ) {
				$adminList[$uid]['roles'][$rid] = $roleList[$rid];
			}
		}
		$temp['list'] = $adminList;
		$this->showView('admin/system/manager_index',$temp);
	}
	
	public function add(){
		if( empty($_POST) ){
			$this->_showEdit();
			return ;
		}
		$input = $this->_getEditData();
		$admin['username'] = $input['username'];
		$admin['password'] = md5($input['password']);
		$admin['school_id']=!empty($input['school_id'])?implode(',',$input['school_id']):0;
		$roleIdList = $input['roleIdList'];
		$adminID = $this->admin->insert($admin);
		if( $adminID ){
			$res = $this->admin->addRole2Admin($adminID, $roleIdList);
		}
		if( $adminID && $res ){
			alert('增加管理员成功',ADMIN_DIR.'manager/index');
		}else{
			alert('增加管理员失败',ADMIN_DIR.'manager/index');
		}
	}
	
	public function update(){
		$adminID = (int)$this->input->get('aid',TRUE);
		// 获取管理员
		if( $adminID ){
			$admin = $this->admin->getOneById($adminID,'username,school_id');
			$admin['school_id']=explode(',',$admin['school_id']);
		}
		if ( empty($admin) ) {
			alert('无用户','back');
			exit;
		}
		// 获取管理员的角色ID列表
		$admin['roleIdList'] = $this->admin->getRoleIdListByAdminID($adminID);
		if( empty($_POST) ){
			$temp['admin'] = $admin;
			$this->_showEdit($temp);
			return ;
		}
		// 获取输入数据
		$input = $this->_getEditData();
		$res = 0;
		$input['school_id'] =!empty($input['school_id'])?implode(',',$input['school_id']):0;
		if( ($admin['username'] != $input['username'] )||($admin['school_id'] != $input['school_id']) ){
			$where  = array();
			$update = array();
			$where['id'] = $adminID;
			$update['username'] = $admin['username'] != $input['username']?$input['username']:$admin['username'];
			$update['school_id'] = $admin['school_id'] != $input['school_id']?$input['school_id']:$admin['school_id'];
			$this->admin->update($where, $update) OR $res+=1;
		}
		if ( $admin['roleIdList'] != $input['roleIdList'] ) {
			$addRoleIdList = array_diff($input['roleIdList'], $admin['roleIdList']);
			$delRoleIdList = array_diff($admin['roleIdList'], $input['roleIdList']);
			if ( !empty($addRoleIdList) ) {
				$this->admin->addRole2Admin($adminID, $addRoleIdList) OR $res+=2;
			}
			if ( !empty($delRoleIdList) ) {
				$this->admin->deleteRole2Admin($adminID, $delRoleIdList) OR $res+=4;
			}
		}
		
		if( 0===$res ){
			alert('修改管理员资料成功',ADMIN_DIR.'manager/index');
		}else{
			alert('修改管理员资料失败:'.$res,ADMIN_DIR.'manager/index');
		}
	}
	
	public function delete(){
		$adminID = (int)$this->input->get('aid',TRUE);
		if ( empty($adminID) ) {
			alert('无用户','back');
			exit;
		}
		
		$res = $this->admin->deleteAdmin($adminID);
		if( $res ){
			alert('删除管理员成功',ADMIN_DIR.'manager/index');
		}else{
			alert('删除管理员失败:'.$res,ADMIN_DIR.'manager/index');
		}
	}
	
	private function _getEditData(){
		$input = &$_REQUEST;
		if( empty($input) || empty($input['username']) ){
			alert('请输入用户名','back');
			exit;
		}
		$input['username'] = trim($input['username']);
		$adminID = isset($input['aid']) ? (int)$input['aid'] : NULL;
		if( $this->admin->checkUsernameIsExists($input['username'],$adminID) ){
			alert('用户名已存在','back');
			exit;
		}
		
		return $input;
	}
	private function _showEdit($temp=array()){
		$roleList = $this->role->getList();
		$schoolList = $this->school->getList();
		
		$temp['roleList'] = $roleList;
	
		$temp['schoolList'] = $schoolList;
		
		$this->showView('admin/system/manager_edit',$temp);
	}
	/**
	 * 清楚管理员缓存信息
	 */
	public function del_cache(){
		if( empty($_POST) ){
			$list = $this->admin->getList();
			$temp['list'] = $list;
			$this->load->view('admin/system/manager_del_cache',$temp);
			return ;
		}
		$idList = $_REQUEST['admins'];
		if( $this->admin->delCache($idList) ){
			alert('删除缓存信息成功',ADMIN_DIR.'manager/index');
		}else{
			alert('删除缓存信息失败',ADMIN_DIR.'manager/index');
		}
	}
	

	
    /**
     * 修改密码
     */
	public function modify_psw(){
		$admin = $this->admin->getUser();
		if( empty($admin) ){
			alert('未登录！',ADMIN_DIR.'login/login');
		}
		
		if( empty($_POST) ){
			$temp['admin'] = $admin;
			$this->showView('admin/system/manager_mdf_psw',$temp);
			return ;
		}

		$input = &$_REQUEST;
		$oldPsw = $input['op'];
		$newPsw = $input['np'];
		$check  = $input['ntp'];

		if( $check != $newPsw ){
			alert('两次输入不一致！',ADMIN_DIR.'manager/modify_psw');
		}
		
		$where = array('id'=>$admin['id']);
		$admin = $this->admin->getOne($where);

		if( md5($oldPsw) != $admin['password'] ){
			alert('旧密码不正确！',ADMIN_DIR.'manager/modify_psw');
			return ;
		}

		$update = array('password'=>md5($newPsw));
		$where = array('id'=>$admin['id']);
		if($this->admin->update($where,$update)){
			$this->admin->logout();
			alert('修改密码成功',ADMIN_DIR.'login/login');
		}else{
			alert('修改密码失败',ADMIN_DIR.'manager/modify_psw');
		}
	}
	
	public function test(){
		// 获取该管理员有权限的栏目列表
		$menuList = $this->admin->getVisibleMenuList(10);
		$menuList = Category::unlimitedForLevel($menuList);
		foreach( $menuList as $v ){
			echo $v['html'],$v['id'],$v['title'],'<br>';
		}
	}
	

	

}
